Subject: Re: How the hell does multitasking work?
From: [email-masked] (Udo K Schuermann)
Date: 5 May 1994 21:00:40 GMT
Newsgroups: comp.sys.amiga.programmer

In article <[email-masked]>, Mike Austin <[email-masked]> wrote:
> How does exec 'let' on execute it's instructuins for a while then 'let' the
> other process have some time?  To execute an instruction don't you have to
> jump to that location?  If you did that you would 'become' that processs
> and not end until you're done...

How Preemptive Multitasking Works:

1. A part of the Operating System (Exec on the Amiga) maintains a list of
   all tasks/processes that are waiting for a CPU.  This is the Ready
   Queue.  It also has a list of all processes currently running, this is
   the Run Queue.  One a single CPU system, there may be many processes in
   the Ready, but only one in the Run Queue.

2. Now, the OS (Exec) uses a timer on the Running Task (Run Queue) and when
   the task's time is up, it saves the so-called CPU State (PC, SP, SR, all
   the registers) in the TCB (Task Control Block), takes it out of the Run
   Queue, and moves the next TCB out of the Ready into the Run Queue.
      The PC, SP, SR, and registers are loaded from the new task's TCB and
   processing continues where it left off the last time that this task was
   suspended.
      This is the task-swapping process and represents the sole overhead in
   Exec (AFAIK.)  In fact, when a task's time is up (i.e. its quantum has
   expired) it is necessary to task-swap ONLY IF ANOTHER TASK IS IN THE
   WAIT QUEUE.  If the Wait Queue is empty, then the task is NOT swapped
   and does NOT lose the CPU.  This is why a system with twenty running
   programs, all waiting for an event, consume NO CPU time and can let
   another task (even at lower priority) run at FULL SPEED.

And there you have it.  Things get trickier when you deal with prioritized
queues (i.e. multiple Ready Queues, one for each priority level) and when
you use semaphores and suspended tasks (tasks waiting for signals and
events (such as Wait()ing for a mouse click, keypress or disk insert) are
stored in a Wait Queue unique to each possible event, semaphore, signal,
etc.

Complications abound, but that should give everyone an idea what's
happening inside Exec and most other such setups.

We now return you to the frightened ramblings of people without nerve.

 ._.  Udo Schuermann                  Someone kick the "fat lady"
 ( )  [email-masked]              before she starts to sing!